package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Criticism;
import com.skyolder.milkbee.service.CriticismService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Criticism.
 */
@RestController
@RequestMapping("/api")
public class CriticismResource {

    private final Logger log = LoggerFactory.getLogger(CriticismResource.class);
        
    @Inject
    private CriticismService criticismService;

    /**
     * POST  /criticisms : Create a new criticism.
     *
     * @param criticism the criticism to create
     * @return the ResponseEntity with status 201 (Created) and with body the new criticism, or with status 400 (Bad Request) if the criticism has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/criticisms")
    @Timed
    public ResponseEntity<Criticism> createCriticism(@RequestBody Criticism criticism) throws URISyntaxException {
        log.debug("REST request to save Criticism : {}", criticism);
        if (criticism.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("criticism", "idexists", "A new criticism cannot already have an ID")).body(null);
        }
        Criticism result = criticismService.save(criticism);
        return ResponseEntity.created(new URI("/api/criticisms/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("criticism", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /criticisms : Updates an existing criticism.
     *
     * @param criticism the criticism to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated criticism,
     * or with status 400 (Bad Request) if the criticism is not valid,
     * or with status 500 (Internal Server Error) if the criticism couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/criticisms")
    @Timed
    public ResponseEntity<Criticism> updateCriticism(@RequestBody Criticism criticism) throws URISyntaxException {
        log.debug("REST request to update Criticism : {}", criticism);
        if (criticism.getId() == null) {
            return createCriticism(criticism);
        }
        Criticism result = criticismService.save(criticism);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("criticism", criticism.getId().toString()))
            .body(result);
    }

    /**
     * GET  /criticisms : get all the criticisms.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of criticisms in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/criticisms")
    @Timed
    public ResponseEntity<List<Criticism>> getAllCriticisms(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Criticisms");
        Page<Criticism> page = criticismService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/criticisms");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /criticisms/:id : get the "id" criticism.
     *
     * @param id the id of the criticism to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the criticism, or with status 404 (Not Found)
     */
    @GetMapping("/criticisms/{id}")
    @Timed
    public ResponseEntity<Criticism> getCriticism(@PathVariable Long id) {
        log.debug("REST request to get Criticism : {}", id);
        Criticism criticism = criticismService.findOne(id);
        return Optional.ofNullable(criticism)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /criticisms/:id : delete the "id" criticism.
     *
     * @param id the id of the criticism to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/criticisms/{id}")
    @Timed
    public ResponseEntity<Void> deleteCriticism(@PathVariable Long id) {
        log.debug("REST request to delete Criticism : {}", id);
        criticismService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("criticism", id.toString())).build();
    }

}
